# coding=utf-8
import random

import time

from core.m_logging import logger
from middlewares.middleware import Middleware


class RandomSleepMiddleware(Middleware):
    def __init__(self, min_sleeping=0, max_sleeping=1):
        super(RandomSleepMiddleware, self).__init__(self._inner_process)
        self.min_sleeping = min_sleeping
        self.max_sleeping = max_sleeping

    def _inner_process(self, request):
        sleeping = random.random() * (self.max_sleeping - self.min_sleeping) + self.min_sleeping  # sleeping 1 to 3 seconds
        logger.info("sleeping random %s seconds..." % sleeping)
        time.sleep(sleeping)
